package mafiagame.roles;

import java.util.List;

import mafiagame.Action;
import mafiagame.Game;
import mafiagame.Mark;
import mafiagame.RoleDefinition;
import mafiagame.Mark.MarkScope;
import mafiagame.Mark.MarkType;
import mafiagame.RoleDefinition.DefinitionType;
import mafiagame.roles.Detective.DetectiveRole;
import mafiagame.Player;
import mafiagame.Role;

public class Driver extends RoleDefinition
{
	public DefinitionType getDefinitionType()
	{
		return DefinitionType.MANIPULATIVE;
	}

	public String explainRole()
	{
		return "You are the driver. You help people get around, and your role will in fact allow you to "
				+ "trade all kills and saves that will happen to two people. Choose wisely...";
	}

	public Driver()
	{
		super();
	}

	public void assignRole(Game g, Player p)
	{
		p.roles.add(new DriverRole(g, p));
	}
	public String getName()
	{
		return "DRIVER";
	}

	public static class DriverRole extends Role
	{
		public class DriveAction extends Action
		{
			@Override
			public String getDescription()
			{
				return "This will allow you to <b>drive</b> two players.";
			}

			@Override
			public String getCommand()
			{
				return "drive";
			}

			@Override
			public boolean Execute(String message)
			{
				String[] tokens = message.split(" ");
				if (tokens.length != 3)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"The drive must be of the format 'drive [number] [number]'");
					return false;
				}
				Player p1 = myGame.parsePlayer(tokens[1]);
				Player p2 = myGame.parsePlayer(tokens[2]);
				if (p1 == null)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You must choose a player who is actually playing.");
					return false;
				}
				if (!p1.living)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You must choose a player who is alive.");
					return false;
				}

				if (p1 == myPlayer)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You cannot drive yourself.");
					return false;
				}

				if (p2 == null)
				{
					myGame.communicator
							.sendPrivateMessage(myPlayer.username,
									"You must choose a player who is actually playing.");
					return false;
				}
				if (!p2.living)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You must choose a player who is alive.");
					return false;
				}

				if (p2 == myPlayer)
				{
					myGame.communicator.sendPrivateMessage(myPlayer.username,
							"You cannot drive yourself.");
					return false;
				}

				Mark m = new Mark();
				m.type = MarkType.REDIRECT_KILL;
				m.redirectTarget = p2;
				m.duration = 2;
				m.scope = MarkScope.BOTH;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.source = myPlayer;
				p1.marks.add(m);

				m = new Mark();
				m.type = MarkType.REDIRECT_KILL;
				m.redirectTarget = p1;
				m.duration = 2;
				m.scope = MarkScope.BOTH;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.source = myPlayer;
				p2.marks.add(m);

				m = new Mark();
				m.type = MarkType.REDIRECT_SAVE;
				m.redirectTarget = p1;
				m.duration = 2;
				m.scope = MarkScope.BOTH;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.source = myPlayer;
				p2.marks.add(m);

				m = new Mark();
				m.type = MarkType.REDIRECT_SAVE;
				m.redirectTarget = p2;
				m.duration = 2;
				m.scope = MarkScope.BOTH;
				m.isBlockable = true;
				m.isRedirectable = false;
				m.source = myPlayer;
				p1.marks.add(m);

				myGame.communicator.sendPrivateMessage(myPlayer.username,
						"Driving <b>" + p1.username + "</b> and " + "<b>"
								+ p2.username + "</b> tonight!");

				return true;
			}

			@Override
			public boolean isPublic()
			{
				return false;
			}

		}

		public DriverRole(Game g, Player p)
		{
			super(g, p);
		}

		public String getName()
		{
			return "DRIVER";
		}

		public String getHelp()
		{
			return "You are the driver. You help people get around, and your role will in fact allow you to "
					+ "trade all kills and saves that will happen to two people. Choose wisely...";
		}

		@Override
		public void SetupPhase()
		{

		}

		@Override
		public void NightPhase()
		{
			myGame.addAction(myPlayer.username, new DriveAction(),
					false);
		}

		@Override
		public void DayPhase()
		{

		}
	}
}
